<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class HomeController extends BaseController {

	public function counts() {
		$list = ['items', 'comments', 'users', 'notes'];
		foreach ($list as $key => $value) {
			$list[$value] = Cache::remember($value . ':nums', config('blog.cache.time'), function () use ($value) {
				return $this->formatNums(DB::table($value)->selectRaw('status, count(id) as num')->groupBy('status')->get());
			});
			unset($list[$key]);
		}
		return $this->response->withSuccess($list);
	}

	public function formatNums($data) {
		$list = [
			'show' => 0,
			'hidden' => 0,
		];
		if ($data) {
			foreach ($data as $key => $value) {
				$value->status == 0 ? $list['hidden'] = $value->num : $list['show'] = $value->num;
			}
		}
		return $list;
	}
}
